﻿$(function () {
    toastr.options.positionClass = 'toast-bottom-center';
    var vm = new Vue({
        el: '#app',
        components: {
            'code-mirror': CodeComponent
        },
        data() {
            var vm = this;
            var interfaceCheck = (rule, value, callback)=>{
                if (vm.textTemplate.type === 0) {
                    if (!value) {
                        return callback(new Error('填写服务实现的接口'))
                    }
                }
                callback();
            };

            var typeNameCheck = (rule, value, callback)=>{
                if (vm.textTemplate.type === 0) {
                    if (!value) {
                        return callback(new Error('填写服务类名'))
                    }
                };
                callback();
            };

            return {
                // 角色与权限
                textTemplatePermission: false,
                createPermission: false,
                deletePermission: false,
                editPermission: false,

                // 主页面
                filterName: null,
                filterType: null,
                dialogVisible: false,
                dialogTitle: '创建在线服务',
                actionType: '创建',
                tableData: [],
                formLabelWidth: '80px',
                typeOptions: [],

                textTemplate: {
                    id: null,
                    name: null,
                    type: 0,
                    typeName: null,
                    interface: null,
                    content: null,
                    isActive: true
                },

                // 验证规则
                rules: {
                    name: [
                        { required: true, message: '请输入名称', trigger: 'blur' }
                    ],
                    content: [
                        { required: true, message: '请输入模板内容', trigger: 'blur' }
                    ],
                    typeName: [
                        { validator: typeNameCheck, trigger:'blur' }
                    ],
                    interface: [
                        { validator: interfaceCheck, trigger: 'blur' }
                    ]
                }
            }
        },
        computed: {
            language: function () {
                var cvm = this; 
                var lang;
                switch (cvm.textTemplate.type) {
                    case 0:
                        lang= 'text/x-csharp';
                        break;

                    case 1:
                        lang = 'application/json';
                        break;

                    case 2:
                        lang = 'plaintext';
                        break;

                    default:
                        lang = 'text/x-csharp';
                        break;
                }
                return lang;
            }
        },
        methods: {

            // 加载当前用户角色和权限.
            loadPermission() {
                var vm = this;
                return new Promise((resolve, reject) => {
                    if (abp.auth.isGranted('BaseSettingGroup.TextTemplates')) { // 设备类型页面权限
                        vm.textTemplatePermission = true;
                        if (abp.auth.isGranted('BaseSettingGroup.TextTemplates.Add')) {// 创建设备配型
                            vm.createPermission = true;
                        }

                        if (abp.auth.isGranted('BaseSettingGroup.TextTemplates.Delete')) { // 删除设备类型
                            vm.deletePermission = true;
                        }

                        if (abp.auth.isGranted('BaseSettingGroup.TextTemplates.Edit')) { // 编辑设备类型
                            vm.editPermission = true;
                        }
                    }
                    resolve();
                });
            },

            // 加载表格数据
            loadTableData() {
                var vm = this;
                return new Promise((resolve) => {
                    abp.ajax({
                        type: 'get',
                        url: `/api/text-template/get-list?filterName=${vm.filterName}&filterType=${vm.filterType}`
                    }).then((res) => {
                        if (res.code === 0) {
                            vm.tableData = res.data;
                           
                        }
                        else {
                            abp.message.error(res.msg);
                        }
                        resolve();
                    });
                })
            },

            // 加载设备类型详情
            getTextTemplateDetail(id) {
                var vm = this;
                abp.ajax({
                    type: 'get',
                    url: `/api/text-template/get?id=${id}`
                }).then(res => {
                    if (res.code === 0) {
                        vm.textTemplate = res.data;
                    }
                    else {
                        vm.dialogVisible = false;
                        abp.message.error(res.msg);
                    }
                });
            },

            // 删除单个
            deleteTextTemplate(id) {
                var vm = this;
                abp.message.confirm('删除文本模板，这可能造成未知异常')
                    .then(confirmed => {
                        if (confirmed) {
                            abp.ajax({
                                type: 'get',
                                url: `/api/text-template/delete?id=${id}`
                            }).then(res => {
                                if (res.code === 0) {
                                    abp.notify.success(res.msg);
                                    vm.loadTableData();
                                }
                            })
                        }
                    })
            },

            // 显示创建对话框
            showCreate() {
                var vm = this;
                vm.actionType = '创建';
                vm.dialogTitle = '新建文本模板';
                vm.dialogVisible = true;
                vm.textTemplate.isActive = true;
                vm.textTemplate.type = 0;
                vm.textTemplate.value = null;
            },

            // 提交创建
            createSubmit() {
                var vm = this;
                vm.$refs['textTemplateForm'].validate((valid) => {
                    if (valid) {
                        abp.ajax({
                            type: 'post',
                            url: `/api/text-template/submit-create`,
                            data: JSON.stringify(vm.textTemplate)
                        }).then(res => {
                            if (res.code == 0) {
                                vm.loadTableData().then(() => {
                                    vm.resetForm();
                                    abp.notify.success(res.msg);
                                })
                            }
                            else {
                                vm.dialogVisible = false;
                                abp.message.error(res.msg).then(() => {
                                    vm.dialogVisible = true;
                                })
                            }
                        });
                    }
                });

            },

            // 显示修改对话框
            showEdit(index, row) {
                var vm = this;
                vm.actionType = '编辑';
                vm.dialogTitle = '编辑';
                vm.dialogVisible = true;
                vm.getTextTemplateDetail(row.id);
            },

            // 提交修改
            editSubmit() {
                var vm = this;
                vm.$refs['textTemplateForm'].validate((valid) => {
                    if (valid) {
                        abp.ajax({
                            type: 'post',
                            url: '/api/text-template/edit-submit',
                            data: JSON.stringify(vm.textTemplate)
                        }).then((res) => {
                            if (res.code === 0) {
                                vm.loadTableData().then(() => {
                                    abp.notify.success(res.msg);
                                })
                            }
                            else {
                                vm.dialogVisible = false;
                                abp.notify.error(res.msg).then(() => {
                                    vm.dialogVisible = true;
                                });
                            }
                        })
                    }
                });

            },

            // 保存
            save() {
                var vm = this;
                if (vm.actionType === '创建') {
                    vm.createSubmit()
                }
                else {
                    vm.editSubmit();
                }
            },

            // 表单重置
            resetForm() {
                var vm = this;
                vm.$refs['textTemplateForm'].resetFields();
                vm.dialogVisible = false;
                vm.textTemplate.id = null;
                vm.textTemplate.name = null;
                vm.textTemplate.type = 0;
                vm.textTemplate.typeName = null;
                vm.textTemplate.interface = null,
                vm.textTemplate.content = '';
            },

            // 加载类型选项
            loadTypeOptions() {
                var vm = this;
                abp.ajax({
                    type: 'get',
                    url: `/api/text-template/get-type-options`
                }).then(res => {
                    if (res.code === 0) {
                        vm.typeOptions = res.data;
                    }
                })
            }
        },
        created() {
            var vm = this;
            vm.loadTypeOptions();
            vm.loadPermission().then(() => {
                vm.loadTableData();
            })
        },
        mounted() {
        }
    });
});